﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.ServiceProcess;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Threading;
using System.ServiceModel;
using Quartz;
using Quartz.Impl;
using log4net;

namespace Hangjing.JobServer
{
    partial class MainService : ServiceBase
    {
        private readonly ILog logger;
        private IScheduler scheduler;
        public MainService()
        {
            InitializeComponent();

            logger = LogManager.GetLogger(GetType());
            ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
            scheduler = schedulerFactory.GetScheduler();
            foreach (var item in scheduler.GetJobGroupNames())
            {
                HJlog.toLog("GetJobGroupNames：" + item);
            }
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                scheduler.Start();
            }
            catch (Exception ex)
            {
                HJlog.toLog("Quartz服务启动失败：" + ex);
                logger.Error("Quartz服务启动失败：" + ex);
                throw;
            }

            HJlog.toLog("Quartz服务成功启动");
            logger.Info("Quartz服务成功启动");
        }

        protected override void OnStop()
        {
            scheduler.Shutdown();
            base.OnStop();
        }

        protected override void OnShutdown()
        {
            scheduler.Shutdown(false);
            base.OnShutdown();
        }

    }


}
